package com.xgkj.diyiketang.util;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.View;

/**
 * 对话框工具类
 * @author houmiao.xiong
 * @date 2012-8-16
 */
public class DialogUtil {
	
	/**
	 * 创建对话框 （包含Title，Message，SingleBtn） and show
	 * @param title
	 * @param message
	 */
	public static void createDialog(Context context,
			String title,
			String message,
			String btnStr,DialogInterface.OnClickListener listener){

		Builder builder = getAlertDialogBuilder(context);

		AlertDialog dialog = builder.setIcon(-1)
		.setTitle(title)
		.setMessage(message)
		.setPositiveButton(btnStr, listener)
		.create();
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
	}
	

	/**
	 * 创建对话框(包含   TitleImg,Title,Message,PosiBtn,NegaBtn) and show
	 * @param context
	 * @param iconid
	 * @param title
	 * @param message
	 * @param posiStr
	 * @param negaStr
	 * @param positiveClickListener
	 * @param negativeClickListener
	 */
	public static AlertDialog createDialog(Context context,
			int iconid ,
			String title,
			CharSequence message,
			String posiStr,
			String negaStr,
			DialogInterface.OnClickListener positiveClickListener,
			DialogInterface.OnClickListener negativeClickListener){
		
		Builder dialogBuilder = getAlertDialogBuilder(context);
		dialogBuilder.setCancelable(false);
		AlertDialog dialog =  dialogBuilder.setIcon(iconid)
		.setTitle(title)
		.setMessage(message)
		.setPositiveButton(posiStr,positiveClickListener)
		.setNegativeButton(negaStr,negativeClickListener)
		.create();
		
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();	
		
		return dialog;
	}

	/**
	 * 创建对话框（包含   TitleImg,Title,Message,posiBtn）
	 * @param context
	 * @param iconId
	 * @param title
	 * @param message
	 * @param posiStr
	 * @param positiveClickListener
	 */
	public static Dialog createDialog(Context context,
			int iconId,
			String title,
			String message,
			String posiStr,
			DialogInterface.OnClickListener positiveClickListener){

		Builder dialogBuilder = getAlertDialogBuilder(context);
		dialogBuilder.setCancelable(false);
		AlertDialog dialog = dialogBuilder.setIcon(iconId)
		.setTitle(title)
		.setMessage(message)
		.setPositiveButton(posiStr,positiveClickListener)
		.create();
		
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		
		return dialog;
	}

	public static Builder getAlertDialogBuilder(Context context){
    	Builder dialogBuilder = null;
    	if(Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH){
    		dialogBuilder = new Builder(context);
    	}else{
    		dialogBuilder = new Builder(context, AlertDialog.THEME_HOLO_LIGHT);
    	}
        return dialogBuilder;
    }

    
	/**
	 * @author shouli.luo
	 * @version 创建时间：2012-8-29 下午10:29:38
	 * 方法作用：创建自定义有两个button的提示框
	 *	
	 * @param context
	 * @param iconResId
	 * @param titleResId
	 * @param textEntryView
	 * @param okResId
	 * @param cancelResId
	 * @param positiveClickListener
	 * @param negativeClickListener
	 */
	public static AlertDialog showTextEntryDialog(Context context,int iconResId,int titleResId,
			View textEntryView,int okResId,int cancelResId,
			DialogInterface.OnClickListener positiveClickListener,
			DialogInterface.OnClickListener negativeClickListener){
		
         Builder builder =  getAlertDialogBuilder(context);
      
        	 builder.setIcon(iconResId);
        	 builder.setTitle(titleResId);
        	 builder.setView(textEntryView);
        	 builder.setPositiveButton(okResId,positiveClickListener);
             builder.setNegativeButton(cancelResId,negativeClickListener);
           
             AlertDialog dialog =   builder.create();
         	dialog.setCanceledOnTouchOutside(false);
         	dialog.show();
         	
         	return dialog;
	}
	
	/**
	 * 创建只带连接进度描述的进度框 
	 * @param context
	 * @param title
	 * @param msg
	 * @param cancelListener
	 * @return
	 */
	public static ProgressDialog  showProgressDialog(Context context,
			String title,String msg,DialogInterface.OnCancelListener cancelListener){
		

		ProgressDialog dialog = ProgressDialog.show(context,
				title,msg);
		
		dialog.setCancelable(true);
		if(cancelListener != null){
			dialog.setOnCancelListener(cancelListener);	
		}
		dialog.setCanceledOnTouchOutside(false);
		return dialog;
	}
	
	public static ProgressDialog  showProgressDialog(Context context,
			String msg,DialogInterface.OnCancelListener cancelListener){
		

		ProgressDialog dialog = ProgressDialog.show(context,null
				,msg);
		
		dialog.setCancelable(true);
		if(cancelListener != null){
			dialog.setOnCancelListener(cancelListener);	
		}
		dialog.setCanceledOnTouchOutside(false);
		return dialog;
	}
	
	public static ProgressDialog  showProgressDialog(Context context,
            String msg, boolean isCancelable, DialogInterface.OnCancelListener cancelListener){
        

        ProgressDialog dialog = ProgressDialog.show(context,null
                ,msg);
        
        dialog.setCancelable(isCancelable);
        if(cancelListener != null){
            dialog.setOnCancelListener(cancelListener); 
        }
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }
}
